// These patterns I picked up from a bit of web searching and for all I know
// could be quite incorrect. Please email dave@ruske.net if there are other
// patterns that should be supported.
var patterns = new Array ( 
	"###,###,###,###",					// US/British
	"###.###.###.###",					// German
	"### ### ### ###",					// French
	"###'###'###'###",					// Swiss
	"#,##,##,##,##,###",				// Indian
	"####\u5104 ####\u4E07 ####", 		// Japanese/Chinese
	"############"						// no formatting
	);

// A little function to take an integer string, strip out current formatting,
// and reformat it according to a pattern string. '#' characters in the pattern
// are substituted with digits from the integer string, other pattern characters
// are output literally into the returned string.
function formatInteger( integer, pattern )
{
	var result = '';

	integerIndex = integer.length - 1;
	patternIndex = pattern.length - 1;

	while ( (integerIndex >= 0) && (patternIndex >= 0) )
	{
		var digit = integer.charAt( integerIndex );
		integerIndex--;
		
		// Skip non-digits from the source integer (eradicate current formatting).
		if ( (digit < '0') || (digit > '9') )  continue;
	
		// Got a digit from the integer, now plug it into the pattern.
		while ( patternIndex >= 0 )
		{
			var patternChar = pattern.charAt( patternIndex );
			patternIndex--;
			
			// Substitute digits for '#' chars, treat other chars literally.
			if ( patternChar == '#' )
			{
				result = digit + result;
				break;
			}
			else
			{
				result = patternChar + result;
			}
		}
	}
	
	return result;
}